package com.carkeeper.userOrder.service.impl;

import com.carkeeper.pojo.CarBill;
import com.carkeeper.pojo.UserBill;
import com.carkeeper.pojo.UserOrderLog;
import com.carkeeper.subject.feign.SubjectFeign;
import com.carkeeper.subject.pojo.Store;
import com.carkeeper.user.feign.UserFeign;
import com.carkeeper.user.pojo.User;
import com.carkeeper.userOrder.dao.CarBillDao;
import com.carkeeper.userOrder.dao.UserBillDao;
import com.carkeeper.userOrder.dao.UserOrderLogDao;
import com.carkeeper.userOrder.service.CarBillService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangmingxin
 * @version 1.0
 * @date 2020/11/27 1:05
 */
@Service
public class CarBillServiceImpl implements CarBillService {

    @Autowired
    private CarBillDao carBillDao;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private SubjectFeign subjectFeign;
    @Autowired
    private UserBillDao userBillDao;
    @Autowired
    private UserOrderLogDao userOrderLogDao;

    /**
     * 查询订单表
     * @return
     */
    @Override
    public Page<CarBill> findPage(CarBill carBill, Integer pageNum, Integer pageSize) {
        //1.获取条件对象
        Example example = this.createExample(carBill);
        PageHelper.startPage(pageNum,pageSize);//创建分页条件
        //2.调用条件进行查询
        Page<CarBill> orderList = (Page<CarBill>) carBillDao.selectByExample(example);
        return  orderList;
    }

    @Override
    public Map findById(Long id) {
        Map resultMap = new HashMap<>();
        //1.获取订单对象
        CarBill carBill = carBillDao.selectByPrimaryKey(id);
        String code = carBill.getCode();//账单编号
        Long userId = carBill.getUserId();//车主id
        Long storeId = carBill.getStoreId();//商家id
        Long logId = carBill.getLogId();//操作表id
        //将code放入到账单明细表中
        UserBill userBill = new UserBill();//
        userBill.setCode(code);
        User user = userFeign.findById(userId).getData();//获取用户
        Store store= subjectFeign.findById(storeId).getData();//获取商家
        //2.根据账单表查询操作表
        UserOrderLog orderLog = userOrderLogDao.findByOrderId(logId);
        //3.获取账单明细表
        UserBill userBill1 = userBillDao.selectOne(userBill);
        //4.封装对象参数到集合
        resultMap.put("user",user);//付款方
        resultMap.put("store",store);//首款方
        resultMap.put("orderLog",orderLog);//操作表数据
        resultMap.put("userBill1",userBill1);//账单明细表数据
        return resultMap;
    }


    /**
     * 封装条件
     * @param carBill
     * @return
     */
    private Example createExample(CarBill carBill) {
        //1.创建条件对象
        Example example = new Example(CarBill.class);
        Example.Criteria criteria = example.createCriteria();
        //判断账单编号并查询
        if (carBill.getCode()!=null&&!"".equals(carBill.getCode())){
            criteria.andEqualTo("code",carBill.getCode());
        }
        //判断客户名称并查询
        if (carBill.getContact()!=null&&!"".equals(carBill.getContact())){
            criteria.andEqualTo("contact",carBill.getContact());
        }
        //判断账单日期并查询
        if (carBill.getCreateDate()!=null&&!"".equals(carBill.getCreateDate())){
            criteria.andEqualTo("createDate",carBill.getCreateDate());
        }
        //判断账单类型并查询
        if (carBill.getOrderType()!=null&&!"".equals(carBill.getOrderType())){
            criteria.andEqualTo("payStatus",carBill.getOrderType());
        }
        //判断账单状态并查询
        if (carBill.getOrderStatus()!=null&&!"".equals(carBill.getOrderStatus())){
            criteria.andEqualTo("carBillType",carBill.getOrderStatus());
        }
        return example;
    }

}
